package cn.jhc.servlet;

import java.io.IOException;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



import cn.jhc.bean.ShopBean;

import cn.jhc.dao.ConnectionFactory;
import cn.jhc.dao.ShopDAO;

@WebServlet(name = "showallgoods", urlPatterns = { "/showallgoods.do" })
public class ShowAllGoodsServlet extends HttpServlet {
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		String condition = req.getParameter("cond");
		if (null == condition || condition.equals("")) {
			resp.sendRedirect("JiTa.jsp");
			return;

		}
		ShopDAO dao = new ShopDAO();
		
		
		
		if (condition.equals("all")) {
			List<ShopBean> list = null;
			try{
			list = list=dao.getAllTitles();
			}catch (Exception e) {
				e.printStackTrace();
			}
			
			req.setAttribute("titles", list);
			RequestDispatcher dispatcher = req
					.getRequestDispatcher("/showjtgoods.jsp");
			dispatcher.forward(req, resp);
			return;

		} else if (condition.equals("precision")) {
			String title = req.getParameter("title");
			String address = req.getParameter("address");
			String manufacturers = req.getParameter("manufacturers");
			String price = req.getParameter("price");
			if ((null == title || title.equals("")) && (null == address
					|| address.equals(""))
					&& (null == manufacturers || manufacturers.equals(""))
					&& (null == price || price.equals(""))) {
				resp.sendRedirect("JiTa.jsp");
				return;
			}
			List<ShopBean>list=null;
			try{
			list=dao.getprecision(title, price, address, manufacturers);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
			if (list.size()==0) {
				resp.sendRedirect("JiTa.jsp");
				return;
			}
			else{
			req.setAttribute("titles", list);
			RequestDispatcher dispatcher = req
					.getRequestDispatcher("/showjtgoods.jsp");
			dispatcher.forward(req, resp);
			return;
			}

		}
		else if(condition.equals("keyword"))
		{
			String keyword=req.getParameter("keyword");
			if(null==keyword||keyword.equals(""))
			{
				resp.sendRedirect("JiTa.jsp");
				return;
			}
			List<ShopBean>list=null;
			try{
				list=dao.getkeyword(keyword);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
			if (list.size()==0) {
				resp.sendRedirect("JiTa.jsp");
				return;
			}
			else{
			req.setAttribute("titles", list);
			RequestDispatcher dispatcher = req
					.getRequestDispatcher("/showjtgoods.jsp");
			dispatcher.forward(req, resp);
			return;
			}
			
		}
		else {
			resp.sendRedirect("JiTa.jsp");
			return;
		}

	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
}
